#Luke Snyder
#Assignment 4

from sys import exit	## imports the exit command
import string			## imports the string library

class Lexicon():		##defined the Lexicon class
	def __init__(self, openFile):				## Constructor is the main loop
		self._wordList = []						## defines an empty list
		try: 									## trys to open the input file
			self._wordFile = openFile 			## defines the local variable for the infile
		except IOError:							## except the file is not valid or cann't be found
			exit()								## exits the program
		lines = self._wordFile.readlines()		## addes all the lines in the file to a string
		for word in lines:						## loops through each word in the string
			word = word.replace("\n","")		## removes the newline character from the string
			present = self.__contains__(word)	## finds if the current word is already in the list this prevents duplicates
			if present == False:				## if it is not present
				self._wordList.append(word)		## append it to the list
				self.__isCaps__(word)			## checks if caps and adds caps word if not
		self._wordFile.close()					## when the loop is finished close the input file
		
	def __contains__(self, inWord):		## defines the __contains__ function
		for word in self._wordList:		## for each word in the class _wordList
			if word == inWord:			## if the word in list is the inWord
				return True				## returns the True Boolean
		return False					## if through with the loop it is assumed that the word 
										## is not present and returns False
										
	def __isCaps__(self, inWord):						## defines the __isCaps__ function
		_wordLen = (len(inWord) -1) * -1				## finds the length of the in word minus one
		_wordOrig = inWord[0]							## defines the first word of the input word
		_wordCaps = string.upper(_wordOrig)				## converts the first letter to an uppercase letter
		if _wordOrig != _wordCaps:						## if the origional letter is not equal to the second then the word is lowercase
			_listWord = _wordCaps + inWord[_wordLen:]	## defines the word to insert into the list from the caps letter and all of the letters after the first
			self._wordList.append(_listWord)			## appends the word to the global list

		

if __name__ == "__main__":					## the following is unit test code
	Language = Lexicon(file("words.txt"))
	print "Luke" in Language
	print "hello" in Language
	print "gowned" in Language
	print "gownsmen" in Language
